package cn.tedu.reflection;/**
 * @Author 金帮嘉诚 @Date 2021-06-28 星期一 8:27
 */
/*本类用于测试反射*/

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 知识点
 * 获取类的字节码对象
 * 获取公共的：
 * 类名包名全路径名
 * 构造方法
 * 成员方法
 * 成员变量
 * 创建对象
 */
public class TestReflect1 {
    @Test
    public void getConstruct() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class<?> student1 = Student.class;
        Class<?> student2 = Class.forName("cn.tedu.reflection.Student");
        Class<?> student3 = new Student().getClass();

        //全路径名，包名、类名
        System.out.println(student1.getName());
        System.out.println(student1.getPackage().getName());
        System.out.println(student1.getSimpleName());

        //构造方法
        Constructor<?>[] cs = student1.getConstructors();
        for (Constructor c : cs
        ) {
            //第一轮：
            //public cn.tedu.reflection.Student()
            // cn.tedu.reflection.Student
            //[]
            //第二轮：
            //public cn.tedu.reflection.Student(java.lang.String,int)
            //cn.tedu.reflection.Student
            //[class java.lang.String, int]

            System.out.println(c);
            System.out.println(c.getName());
            Class[] pt = c.getParameterTypes();
            System.out.println(Arrays.toString(pt));
        }

        //公共的 普通方法
        Method[] methods = student1.getMethods();
        for (Method m : methods
        ) {
            //注意方法名和参数列表是分开输出
            //toString[]
            //play[int]
            //eat[]
            //wait[]
            //wait[long, int]
            //wait[long]
            //equals[class java.lang.Object]
            //hashCode[]
            //getClass[]
            //notify[]
            //notifyAll[]
            System.out.print(m.getName());
            Class<?>[] pt = m.getParameterTypes();
            System.out.println(Arrays.toString(pt));
        }

        //公共的 成员变量
        Field[] fields = student1.getFields();
        for (Field f : fields
        ) {
            //name
            System.out.println(f.getName());
            Class<?> type = f.getType();
            //java.lang.String
            System.out.println(type.getName());
        }

        //创建对象 两种方法
        //字节码对象创建
        Object o = student1.newInstance();
        //Student{name='null', age=0}
        System.out.println(o);
        //注意向下转型
        //0
        //null
        //play:1
        //Eat
        Student st = (Student) o;
        System.out.println(st.age);
        System.out.println(st.name);
        st.play(1);
        st.eat();

        //构造方法创建
        Constructor<?> c1 = student1.getConstructor(String.class, int.class);
        Object o1 = c1.newInstance("????", 18);
        //Student{name='????', age=18}
        System.out.println(o1);
        Student st1 = (Student) o1;
        //18
        //????
        //play:11
        //Eat
        System.out.println(st1.age);
        System.out.println(st1.name);
        st1.play(11);
        st1.eat();


    }


}
